import http from './http'
import type {
  AuthLoginRequestData,
  AuthLoginResponseData,
  AuthRegisterRequestData,
  AuthRegisterResponseData,
  ConfigResponseData,
  ConfigUserStatusValueType,
  MenuItemResponseData,
  UserPaginationResponseData,
  RoleItemResponseData,
  UserItemRequestData,
  UserItemResponseData,
  UserListItemResponseData,
  UserProfileResponseData
} from '@/types/api'

export const login = async (data: AuthLoginRequestData) => {
  return http.post('/auth/login', data) as Promise<AuthLoginResponseData>
}

export const register = async ({ username, password, email }: AuthRegisterRequestData) => {
  return http.post<AuthRegisterResponseData>('/auth/register', { username, password, email })
}

export const getConfig = async () => {
  return http.get('/config') as Promise<ConfigResponseData>
}

export const getMenus = async () => {
  return http.get('/menus') as Promise<MenuItemResponseData[]>
}

export const getUserProfile = async () => {
  return http.get('/user/profile') as Promise<UserProfileResponseData>
}

export const getRoleList = async () => {
  return http.get('/role/list') as Promise<RoleItemResponseData[]>
}

export const getUserList = async (
  page: number = 1,
  size: number = 20,
  status: ConfigUserStatusValueType = 'All',
  roles: string[] = [],
  keyword: string = ''
) => {
  return http.get('/user/list', {
    params: {
      page,
      size,
      status,
      roles: roles.join(','),
      keyword
    }
  }) as Promise<UserPaginationResponseData<UserListItemResponseData>>
}

export const deleteUser = async (id: string) => {
  return http.delete(`/user/${id}`) as Promise<boolean>
}

export const addUser = async (user: UserItemRequestData) => {
  return http.post(`/user`, user) as Promise<UserItemResponseData>
}

export const updateUser = async (id: string, user: UserItemRequestData) => {
  return http.put(`/user/${id}`, user) as Promise<UserItemResponseData>
}

export const getUser = async (id: string) => {
  return http.get(`/user/${id}`) as Promise<UserItemResponseData>
}
